﻿Option Explicit On
Option Strict On

Partial Class Controls_MyEntitiesControl
    Inherits System.Web.UI.UserControl
    Dim u As MembershipUser
    Dim personId As Int32
    Dim delid As Integer
    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Try
            u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
            Dim getpersonid As New GetPersonIdFromUserId()
            personId = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))

            GridView1.DataBind()
        Catch ex As Exception
            'Dim rowsErr As System.Data.DataRow() = tTable.GetErrors()
            'For i As Integer = 0 To rowsErr.Count() - 1
            'If rowsErr(i).HasErrors Then
            'Dbg.WriteLine(rowsErr(i).RowError)
            'End If
            'Next
        End Try

        'FormView1.DataBind()
    End Sub


    Protected Sub ObjectDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource1.Selecting
        e.InputParameters("PersonId") = personId
    End Sub

    'Protected Sub ObjectDataSource2_Inserted(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource2.Inserted
    '    Dim insertedid As Integer = CInt(e.OutputParameters("EntityId"))

    '    Try
    '        Dim insertpersonentityTA As New LogginsLogDSTableAdapters.usp_PersonEntity_ExpTableAdapter()
    '        insertpersonentityTA.Insert(personId, insertedid, CType(1, Boolean?))

    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    '    GridView1.DataBind()

    'End Sub

    'Protected Sub ObjectDataSource2_Inserting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource2.Inserting
    '    'Dim Name As TextBox = CType(Me.FormView1.FindControl("NameTextBox"), TextBox)
    '    'Dim CompanyRegNo As TextBox = CType(Me.FormView1.FindControl("CompanyRegNoTextBox"), TextBox)
    '    'Dim VATRegNoText As TextBox = CType(Me.FormView1.FindControl("VATRegNoTextBox"), TextBox)
    '    'e.InputParameters("Name") = Name.Text
    '    'e.InputParameters("CompanyRegNo") = CompanyRegNo.Text
    '    'e.InputParameters("VATRegNo") = VATRegNoText.Text

    '    e.InputParameters("DateCreated") = DateTime.Now
    '    e.InputParameters("EntityTypeId") = 0
    '    e.InputParameters("Active") = CType(1, Boolean?)

    'End Sub

    'Protected Sub ObjectDataSource2_Updating(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource2.Updating
    '    'Dim password As TextBox = CType(FormView1.FindControl("tb_password1"), TextBox)
    '    'Dim Name As TextBox = CType(Me.FormView1.FindControl("NameTextBox"), TextBox)
    '    'Dim CompanyRegNo As TextBox = CType(Me.FormView1.FindControl("CompanyRegNoTextBox"), TextBox)
    '    'Dim VATRegNoText As TextBox = CType(Me.FormView1.FindControl("VATRegNoTextBox"), TextBox)
    '    'e.InputParameters("Name") = Name.Text
    '    'e.InputParameters("CompanyRegNo") = CompanyRegNo.Text
    '    'e.InputParameters("VATRegNo") = VATRegNoText.Text
    '    e.InputParameters("DateCreated") = DateTime.Now
    '    e.InputParameters("EntityTypeId") = 0
    '    e.InputParameters("Active") = CType(1, Boolean?)
    'End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        'FormView1.ChangeMode(FormViewMode.Edit)
        TabContainer1.ActiveTabIndex = 0
        Dim EntityTypeId As Int32 = CInt(GridView1.SelectedDataKey.Values().Item("EntityTypeId"))
        Dim EntityIdKey As Int32 = CInt(GridView1.SelectedDataKey.Values().Item("ID"))
        DropDownList2.SelectedValue = CStr(EntityTypeId)
        TextBox1.Text = Server.HtmlDecode(GridView1.SelectedRow.Cells(2).Text)
        TextBox2.Text = Server.HtmlDecode(GridView1.SelectedRow.Cells(3).Text)
        TextBox3.Text = Server.HtmlDecode(GridView1.SelectedRow.Cells(4).Text)
        Dim smssettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_SMSSelectTableAdapter()
        Dim smssettingsDT As LogginsLogDS.usp_EntitySettings_SMSSelectDataTable
        smssettingsDT = smssettingsTA.GetData(Nothing)
        Dim smsid = (From sms In smssettingsDT.AsEnumerable() Where sms.EntityId = EntityIdKey Select sms).FirstOrDefault()
        If smsid Is Nothing Then
            txtSmsSenderNameUpdate.Text = String.Empty
            txtSmsOriginatorUpdate.Text = String.Empty
            chkSMSTrackItemsUpdate.Checked = False
            smssettingsTA.Insert(EntityIdKey, chkTrackItemsInsert.Checked, txtSmsSenderNameInsert.Text, txtSmsOriginatorInsert.Text, CType(1, Boolean?))

        Else
            txtSmsSenderNameUpdate.Text = smsid.SenderName
            txtSmsOriginatorUpdate.Text = smsid.SMSOriginator
            chkSMSTrackItemsUpdate.Checked = smsid.TrackItems
        End If

        Dim emailsettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_EMAILSelectTableAdapter()
        Dim emailsettingsDT As LogginsLogDS.usp_EntitySettings_EMAILSelectDataTable
        emailsettingsDT = emailsettingsTA.GetData(Nothing, EntityIdKey)
        Dim emailid = (From emil In emailsettingsDT.AsEnumerable() Where emil.EntityId = EntityIdKey Select emil).FirstOrDefault()

        If emailid Is Nothing Then
            txtSenderNameUpdate.Text = String.Empty
            txtSenderEmailUpdate.Text = String.Empty
            ddlOptInMethodUpdate.SelectedValue = CStr(1)
            chkEmailTrackItemsUpdate.Checked = False
            emailsettingsTA.Insert(EntityIdKey, txtEmailSenderNameInsert.Text, txtEmailSenderNameInsert.Text, CType(ddlOptInMethodOnsert.SelectedValue, Byte?), chkEmailTrackItemsInsert.Checked, CType(1, Boolean?))
        Else
            'For Each emailsRow As LogginsLogDS.usp_EntitySettings_EMAILSelectRow In emailsettingsDT
            If emailid.IsSenderNameNull Then
                txtSenderNameUpdate.Text = Nothing
            Else
                txtSenderNameUpdate.Text = emailid.SenderName
            End If
            If emailid.IsSenderEmailNull Then
                txtSenderEmailUpdate.Text = Nothing
            Else
                txtSenderEmailUpdate.Text = emailid.SenderEmail
            End If

            ddlOptInMethodUpdate.SelectedValue = CStr(emailid.OptInMethod)
            chkEmailTrackItemsUpdate.Checked = emailid.TrackItems
            'Next

        End If

    End Sub

    'Protected Sub ObjectDataSource2_Deleting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource2.Deleting
    '    delid = CInt(e.InputParameters("EntityID"))
    'End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If Not NameTextBox.Text.Length <> 0 Then
            lblInsertNameRequired.Text = "Name is Required field."
        Else
            ObjectDataSource3.Insert()
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub ObjectDataSource3_Inserting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource3.Inserting
        e.InputParameters("Name") = NameTextBox.Text
        e.InputParameters("CompanyRegNo") = CompanyRegNoTextBox.Text
        e.InputParameters("VATRegNo") = VATRegNoTextBox.Text

        e.InputParameters("DateCreated") = DateTime.Now
        e.InputParameters("EntityTypeId") = DropDownList1.SelectedValue
        e.InputParameters("Active") = CType(1, Boolean?)
    End Sub

    Protected Sub ObjectDataSource3_Inserted(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource3.Inserted
        Dim insertedid As Integer = CInt(e.OutputParameters("EntityId"))
        Try
            Dim insertpersonentityTA As New LogginsLogDSTableAdapters.usp_PersonEntity_ExpTableAdapter()
            insertpersonentityTA.Insert(personId, insertedid, CType(1, Boolean?))
            Dim smssettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_SMSSelectTableAdapter()
            smssettingsTA.Insert(insertedid, chkTrackItemsInsert.Checked, txtSmsSenderNameInsert.Text, txtSmsOriginatorInsert.Text, CType(1, Boolean?))
            Dim emailsettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_EMAILSelectTableAdapter()
            emailsettingsTA.Insert(insertedid, txtEmailSenderNameInsert.Text, txtEmailSenderNameInsert.Text, CType(ddlOptInMethodOnsert.SelectedValue, Byte?), chkEmailTrackItemsInsert.Checked, CType(1, Boolean?))
        Catch ex As Exception
            Throw ex
        End Try
        GridView1.DataBind()


    End Sub



    'Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
    '    Dim entityTA As New LogginsLogDSTableAdapters.usp_EntityTableAdapter()
    '    If e.CommandName = "LikeDelete" Then

    '        Dim delnumber As Integer = Convert.ToInt32(e.CommandArgument)

    '        Try
    '            Dim personentitypeexpTA As New LogginsLogDSTableAdapters.SPTableAdapter()
    '            personentitypeexpTA.usp_PersonEntity_ExpDelete_ByPersonEntityId(personId, delnumber)
    '        Catch ex As Exception
    '            Throw ex
    '        End Try
    '        Try

    '            entityTA.Delete(delnumber)
    '        Catch ex As Exception
    '            Throw ex
    '        End Try


    '        GridView1.DataBind()
    '        'ElseIf e.CommandName = "LikeUpdate" Then
    '        '    Dim Name As TextBox = CType(Me.FormView1.Row.FindControl("NameTextBox"), TextBox)
    '        '    Dim CompanyRegNo As TextBox = CType(Me.FormView1.Row.FindControl("CompanyRegNoTextBox"), TextBox)
    '        '    Dim VATRegNoText As TextBox = CType(Me.FormView1.Row.FindControl("VATRegNoTextBox"), TextBox)
    '        '    entityTA.Update(Name.Text, CompanyRegNo.Text, DateTime.Now, VATRegNoText.Text, 0, True, CType(GridView1.SelectedValue, Integer?))
    '    End If
    'End Sub


    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If Not TextBox1.Text.Length <> 0 Then
            lblEditNameRequired.Text = "Name is Required field."
        Else
            'ObjectDataSource3.Insert()
            'GridView1.DataBind()
            lblEditNameRequired.Text = ""
            If Not GridView1.SelectedValue Is Nothing Then
                Dim entityTA As New LogginsLogDSTableAdapters.usp_EntityTableAdapter()
                entityTA.Update(TextBox1.Text, TextBox2.Text, DateTime.Now, TextBox3.Text, CType(DropDownList2.SelectedValue, Integer?), True, CType(GridView1.SelectedValue, Integer?))
                Dim EntityIdKey As Int32 = CInt(GridView1.SelectedDataKey.Values().Item("ID"))
                Dim smssettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_SMSSelectTableAdapter()
                Dim smssettingsDT As LogginsLogDS.usp_EntitySettings_SMSSelectDataTable
                smssettingsDT = smssettingsTA.GetData(Nothing)
                Dim emailsettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_EMAILSelectTableAdapter()
                Dim emailsettingsDT As LogginsLogDS.usp_EntitySettings_EMAILSelectDataTable
                emailsettingsDT = emailsettingsTA.GetData(Nothing, EntityIdKey)

                Dim smsid = (From sms In smssettingsDT.AsEnumerable() Where sms.EntityId = EntityIdKey Select sms).FirstOrDefault()
                Dim emailid = (From emails In emailsettingsDT.AsEnumerable() Where emails.EntityId = EntityIdKey Select emails).FirstOrDefault()


                smssettingsTA.Update(smsid.Id, EntityIdKey, chkSMSTrackItemsUpdate.Checked, txtSmsSenderNameUpdate.Text, txtSmsOriginatorUpdate.Text, CType(1, Boolean?))
                emailsettingsTA.Update(emailid.Id, EntityIdKey, txtSenderNameUpdate.Text, txtSenderEmailUpdate.Text, CType(ddlOptInMethodOnsert.Text, Byte?), chkEmailTrackItemsInsert.Checked, CType(1, Boolean?))
                'For Each emailRow As LogginsLogDS.usp_EntitySettings_EMAILSelectRow In emailsettingsDT
                '    emailsettingsTA.Update(emailRow.Id, EntityIdKey, txtSenderNameUpdate.Text, txtSenderEmailUpdate.Text, CType(txtOptInMethodUpdate.Text, Byte?), chkEmailTrackItemsInsert.Checked, CType(1, Boolean?))
                'Next
                Session("EntityID") = Nothing

                Response.Redirect("~/private/secure_members/manage/MyEntity.aspx")
            Else
                lblError.Text = "Please select entity/objects list"
            End If
        End If

    End Sub

    Protected Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        If GridView1.SelectedDataKey Is Nothing Then
            MsgBox("Please select entity to delete.", MsgBoxStyle.ApplicationModal, "")
        Else
            Dim entityTA As New LogginsLogDSTableAdapters.usp_EntityTableAdapter()
            Dim delnumber As Integer = Convert.ToInt32(GridView1.SelectedValue)


            Dim personentitypeexpTA As New LogginsLogDSTableAdapters.SPTableAdapter()
            personentitypeexpTA.usp_PersonEntity_ExpDelete_ByPersonEntityId(personId, delnumber)
            Dim EntityIdKey As Int32 = CInt(GridView1.SelectedDataKey.Values().Item("ID"))
            Dim smssettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_SMSSelectTableAdapter()
            Dim smssettingsDT As LogginsLogDS.usp_EntitySettings_SMSSelectDataTable
            smssettingsDT = smssettingsTA.GetData(Nothing)
            Dim smsid = (From sms In smssettingsDT.AsEnumerable() Where sms.EntityId = EntityIdKey Select sms).FirstOrDefault()
            smssettingsTA.Delete(smsid.Id)
            Dim emailsettingsTA As New LogginsLogDSTableAdapters.usp_EntitySettings_EMAILSelectTableAdapter()
            Dim emailsettingsDT As LogginsLogDS.usp_EntitySettings_EMAILSelectDataTable
            emailsettingsDT = emailsettingsTA.GetData(Nothing, EntityIdKey)
            For Each emailRow As LogginsLogDS.usp_EntitySettings_EMAILSelectRow In emailsettingsDT
                emailsettingsTA.Delete(emailRow.Id)
            Next
            Try
            Catch ex As Exception
                Throw ex
            End Try
            Try

                entityTA.Delete(delnumber)
                Session("EntityID") = Nothing
            Catch ex As Exception
                Throw ex
            End Try
            GridView1.DataBind()

        End If



    End Sub

    'Protected Sub TabContainer1_ActiveTabChanged(sender As Object, e As System.EventArgs) Handles TabContainer1.ActiveTabChanged
    '    If TabContainer1.ActiveTabIndex = 0 Then
    '        Response.Write("1")
    '        NameTextBox.Text = "1"
    '    End If

    'End Sub
End Class
